package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseActivity;
import com.e2oto.seller.common.base.RecycleViewAdapter;
import com.e2oto.seller.modules.seller.adapter.SelectCityAdapter;
import com.e2oto.seller.modules.seller.adapter.SelectDistrictAdapter;
import com.e2oto.seller.modules.seller.adapter.SelectProvinceAdapter;
import com.e2oto.seller.modules.seller.bean.CityBean;
import com.e2oto.seller.modules.seller.bean.ProvinceBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZhangLiQiang on 2017/11/14.
 */

public class SelectCityActivity extends BaseActivity {

    @BindView(R.id.select_address_rv)
    RecyclerView recyclerView;

    private SelectCityAdapter adapter;

    @Override
    protected void afterView(Bundle savedInstanceState) {
        adapter = new SelectCityAdapter(JSONObject.parseArray(getIntent().getStringExtra("cities"), CityBean.class), this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_address_name, new RecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                adapter.resetStatus();
                adapter.getDataList().get(position).setSelected(true);
                adapter.notifyDataSetChanged();

                Intent intent = new Intent(SelectCityActivity.this, SelectDistrictActivity.class);
                intent.putExtra("districts", JSONObject.toJSONString(adapter.getDataList().get(position).getCounty_array()));
                intent.putExtra("city", adapter.getSelectedCityBean());
                intent.putExtra("province", getIntent().getSerializableExtra("province"));
                startActivity(intent);
            }
        });

    }

    @Override
    protected int getContentView() {
        return R.layout.activity_select_address;
    }

    @Override
    protected void onReceiveRxMessage(RxBusBean anyType) {
        super.onReceiveRxMessage(anyType);
        if (anyType.getType() == 422) {
            finish();
        }
    }
    @OnClick(R.id.toolbar_left_btn)
    public void onClickBack() {
        finish();
    }
}
